{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#循环语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9\n",
      "8\n",
      "7\n",
      "6\n",
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "flag = 10\n",
    "while flag:   # 当条件为真时，进入循环\n",
    "    print(flag)\n",
    "    flag -= 1    # 减到0时，因为0 = flase，所以不执行了，1 - 10都是True，判断能够打印到哪时\n",
    "                 # 有先看边界，把边界带进去看一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # range函数的含义： [0-10），大于等于0，小于10，前包后不包。range() 是一个容器\n",
    "    print (i)        # range() 可以当做计数器使用，里面写 1 ，就是跑一次，里面是几，就跑几次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): \n",
    "    print (i + 1)   # python 不对变量进行修改，i 还是 i ，只是打印的进行了修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 隐含赋值，从range内取一个元素，进行赋值，从里面拿东西的过程，叫遍历\n",
    "    print (i + 1)    # 遍历的过程中不会重复拿。\n",
    "    i = i + 10      # 因为i的值都是从range 中选取的，所以赋值也没有用，第一次给 i 的赋值是 0+10=10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9\n",
      "8\n",
      "7\n",
      "6\n",
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "for i in range (9 , -1 , -1): # 倒序计数，容器的范围是[10 - 1) 永远是左闭右开\n",
    "    print (i + 1)             # 9拿完之后，要到8，这个8是9-1得到的，减去的1，就是后面加的步长。\n",
    "                             #  因为默认是正着走，然后加 -1 的目的是让它反着走。-1的位置叫步长step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 11, 2)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1 , 11 , 2)   # 这种写法是不从0开始走，不到10结束才这样写；\n",
    "                    # 直接执行range得到的时range对象，也叫可迭代对象：\n",
    "                    # 即：从里面能够一个一个取出来的对象，range补遍历不出对象，所以叫惰性对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(1 , 11 , 2):  # 加了for 循环之后才能拿出来\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range (1 , 2)  # 写两个参数的，是边界，而不是步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 取 10 之内的偶数\n",
    "    if i % 2 == 0:\n",
    "        print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 取 10 之内的奇数\n",
    "    if i % 2:\n",
    "        print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 取 10 之内的偶数\n",
    "    if not i % 2:\n",
    "        print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 取 10 之内的偶数\n",
    "    if not i & 1:    # 取余（模）%，和位与运算&，可以作为循环条件，\n",
    "        print (i)     # 结果是 0或1 的都可以作为if语句的条件（True，flase）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range (0 , 10 , 2): # 取 10 之内的偶数,不走循环走步长可以提高工作效率，\n",
    "        print (i)            # 多一次循环就多一次运算。：迭代了五次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# continue中止当次循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in range (10): # 取 10 之内的偶数\n",
    "    if  i & 1 :\n",
    "        continue # 中止当前这一次循环，进了if分支后【能进if分支说明，条件为真（1）】，\n",
    "    print (i)     # 走到 continue 就返回 range 继续"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取100以内7的倍数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "35\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "70\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n"
     ]
    }
   ],
   "source": [
    "for i in range (100): #效率低\n",
    "    if i % 7 == 0:\n",
    "        print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "35\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "70\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n"
     ]
    }
   ],
   "source": [
    "for i in range (0 , 100 , 7):\n",
    "     print (i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#10000以内取 7 的倍数的前20个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "35\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "70\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n",
      "105\n",
      "112\n",
      "119\n",
      "126\n",
      "133\n",
      "140\n"
     ]
    }
   ],
   "source": [
    "start = 7 # start 可以随便换成别的表示\n",
    "for i in range (20): # 这时候的 range 相当于计数器，记了20次数。\n",
    "    print (start)    # 而不是从里面取东西，因为 print 的是 start\n",
    "    start += 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# break 终止当前循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "35\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "70\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n",
      "105\n",
      "112\n",
      "119\n",
      "126\n",
      "133\n",
      "140\n"
     ]
    }
   ],
   "source": [
    "count = 1\n",
    "for i in range(7 , 10000 , 7):\n",
    "    print (i)\n",
    "    if count == 20: # 与if没有关系，终止的是for的循环语句，if只是控制是否执行 break\n",
    "        break      # 终止当前循环体的执行，当前循环就不在执行了，比continue 要强\n",
    "    count += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#给定一个不超过五位的正整数，依照该数的位数，依次打印出万位，千位，百位，十位，个位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num24855\n",
      "2\n",
      "4\n",
      "8\n",
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "a = input (\"num\")\n",
    "b = int(a)\n",
    "c = 10000\n",
    "for i in range(5):           # 有了计数就不需要判断来决定什么时候出来了\n",
    "    print (b // c)           # 12345//10000 ->1 \n",
    "    b = b % c               # 12345 % 10000 ->2345 -> b\n",
    "    c = c // 10            #10000 // 10 -> 1000 -> c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  希望知道数字长度，从第一个不是 0 的数开始从高位到低位打印 \n",
    "#  这种题需要分步骤去写，先实现从高到低打印，在实现不打印高位 0，最后加数字长度计数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n",
      "0\n",
      "4 ...............\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "num = int (\"002340\")\n",
    "lengh = 5\n",
    "w = 10000\n",
    "flag = False                #设一个开关，第一个数为 0 时不进入\n",
    "count = 0\n",
    "while w:\n",
    "    t = num // w\n",
    "    if not flag:          # 进入while循环后，判断才起作用。\n",
    "        if t:\n",
    "            flag = True # 第一个if 执行后，flag变成True\n",
    "        else:\n",
    "            lengh -= 1\n",
    "    if flag:          #第一个if进去后，第二个if 仍然可以执行。\n",
    "        print (t)\n",
    "        count += 1\n",
    "    num = num % w\n",
    "    w = w // 10\n",
    "print (lengh , \"...............\") #这两种都是打印的长度\n",
    "print (count)              #这两种都是打印的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环 else 子句，后期很有用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "else\n"
     ]
    }
   ],
   "source": [
    "for i in range (5):\n",
    "    print (i)\n",
    "else:\n",
    "    print(\"else\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range (5):\n",
    "    print (i)\n",
    "    if i > 3:   # 打印完4之后，i = 4 > 3，会执行 break，直接打断整个循环，此时就不会打印 else\n",
    "        break\n",
    "else:           # 只有正常循环结束是才会执行else，break叫不正常结束循环\n",
    "    print(\"else\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "else\n"
     ]
    }
   ],
   "source": [
    "for i in range (0): # [0 , 0) 相当于是空的\n",
    "    print (i)\n",
    "else:\n",
    "    print(\"else\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "else\n"
     ]
    }
   ],
   "source": [
    "for i in []:        # 直接是空的\n",
    "    print (i)\n",
    "else:              # 循环不出错，循环正常结束就可以执行else，哪怕是空的都可以执行\n",
    "    print(\"else\")  # break是非正常打断，所以不能执行，continue也可以继续执行else"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
